﻿using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class SalaryBonus : IEntityService<LuongThuong>
    {
        public BangLuong _salary;

        public SalaryBonus()
        {
            _salary = SimpleIoc.Default.GetInstance<BangLuong>();
        }

        public List<LuongThuong> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.BangLuongs.Include("LuongThuongs").Where(x => x.MaBangLuong == _salary.MaBangLuong).FirstOrDefault();
                return query.LuongThuongs.ToList();
            }
        }

        public bool Add(LuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entity.MaBangLuong = _salary.MaBangLuong;
                entities.LuongThuongs.Add(entity);
                entities.SaveChanges();
                return true;
            }
        }

        public bool Update(LuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.LuongThuongs.Where(x => x.MaLuongThuong == entity.MaLuongThuong).FirstOrDefault();
                if (query != null)
                {
                    query.LoaiLuongThuong = entity.LoaiLuongThuong;
                    query.SoTien = entity.SoTien;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(LuongThuong entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.LuongThuongs.Where(x => x.MaLuongThuong == entity.MaLuongThuong).FirstOrDefault();
                if (query != null)
                {
                    entities.LuongThuongs.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool DeleteList(List<LuongThuong> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var query = entities.LuongThuongs.Where(x => x.MaLuongThuong == item.MaLuongThuong).FirstOrDefault();
                    if (query != null)
                    {
                        entities.LuongThuongs.Remove(query);
                    }
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<LuongThuong> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.BangLuongs.Include("LuongThuongs").Include("LuongThuongs.LoaiLuongThuong")
                    .Where(x => x.MaBangLuong == _salary.MaBangLuong).FirstOrDefault();
                if (query != null)
                {
                    return query.LuongThuongs.OrderBy(p => p.MaBangLuong).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    throw new Exception("Null Staff");
                }
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            int count = this.GetAll().Count;
            int pages = count / pageSize;
            if (count > pages * pageSize) return pages + 1;
            return pages;
        }
    }
}
